/********************************************************************************
*                 Copyright (C) 2011 - Las Venturas Playground                  *
*                      http://iv-mp.nl | http://lvp-iv.com                      *
*                                                                               *
*    This program is free software: you can redistribute it and/or modify       *
*    it under the terms of the GNU General Public License as published by       *
*    the Free Software Foundation, either version 3 of the License, or          *
*    (at your option) any later version.                                        *
*                                                                               *
*    This program is distributed in the hope that it will be useful,            *
*    but WITHOUT ANY WARRANTY; without even the implied warranty of             *
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
*    GNU General Public License for more details.                               *
*                                                                               *
*    You should have received a copy of the GNU General Public License          *
*    along with this program.  If not, see <http://www.gnu.org/licenses/>.      *
*                                                                               *
*********************************************************************************
*                                                                               *
*  Las Venturas Playground - callbacks/onPlayerCommand.nut                      *
*                                                                               *
*  The onPlayerCommand callback is located here.                                *
*                                                                               *
********************************************************************************/

/**
 * This function bla, to be edited.
 * @author: Pugwipe <pugwipe@gmail.com>
 *
 * @param cmdHandler bla
 */
local commandHandlers = [];
function registerCommandHandler (cmdHandler)
{
    commandHandlers.append (cmdHandler);
}

/**
 * This event is called when a user enters a command.
 *
 * @param playerid The playerid that executed the command
 * @param command The command he typed.
 */
function onPlayerCommand(playerid, command)
{
    local cmdParameters = split(command, " ");
    local found = false;
    
	if(Minigames.Core.getPlayerMinigame (playerid) != null && cmdParameters[0] != "/leave")
	{
		sendPlayerErrorMessage (playerid, "You cannot use commands in minigames. Use /leave to leave the minigame.");          
		return;
	}
    else if (isRegistered[playerid] == true && isLoggedIn[playerid] == false && cmdParameters[0] != "/login")
    {
        sendPlayerErrorMessage (playerid, "You have to login before you can use other commands!");
        return;
    }
    else
    {
        if (isSpawned[playerid] == false)
        {
            sendPlayerErrorMessage (playerid, "You cannot use commands if you haven't spawned yet!");          
            return;
        }
        else
        {
            foreach (cmdHandler in commandHandlers) 
            {
                if (cmdHandler.onCommand (playerid, command) || command == "/login") 
                {
                   found = true;
                }
            }
        }
    }
    
    if (!found) 
    {
        sendPlayerMessage (playerid, "SERVER: Unknown command", COLOR_WHITE);
    }
    
    return true;
}

addEvent("playerCommand", onPlayerCommand);